// pages/partner/storycircle/storycircle.js
var shihan = require('../../../common/fetch')
var config = require('../../../common/config')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    listflag: true,
    has_more: true,
    imgflag: true,
    showLoading: true,
    video_listflag: true,
    video_has_more: true,
    material_list: [],
    video_list: [],
    currentTab: 0,
    playIndex: null,//用于记录当前播放的视频的索引值
    res_navbar: ['一键发圈', '视频']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {//material_list
    if (this.data.currentTab == 1) {
      this.getVideoList();
    } else {
      this.getMaterialList();
    }
    wx.hideShareMenu({})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  onResNavbarTap: function (e) { //collectionrem_list
    this.setData({
      page: 1,
      video_page: 1,
      listflag: true,
      playIndex: null,
      video_listflag: true,
      showLoading: true,
      material_list: [],
      video_list: [],
      currentTab: e.currentTarget.dataset.index
    });
    if (this.data.currentTab == 1) {
      this.getVideoList();
    } else {
      this.getMaterialList();
    }
  },

  getMaterialList: function () {
    var that = this;
    shihan.sendRequest(config.agent_material_list, 'POST', { page: that.data.page, page_size: that.data.page_size })
      .then(function (response) {
        if (response.data.code == 0 && response.data.data.sh_items.length > 0) {
          that.setData({
            listflag: true,
            has_more: true,
            material_list: that.data.material_list.concat(response.data.data.sh_items)
          })
          if (that.data.material_list.length == response.data.data.sh_total_rows) {
            that.setData({
              listflag: false,
              has_more: false
            })
          } 
        } else {
          that.setData({
            listflag: false,
            has_more: false
          })
        }
        that.setData({
          showLoading: false
        })
        wx.stopPullDownRefresh()
      }, function (error) {
        console.log(error);
      });
  },
  getVideoList: function () {
    var that = this
    shihan.sendRequest(config.agent_material_video_list, 'POST', { page: that.data.video_page,page_size: that.data.page_size }, {})
      .then(function (response) {
        for (var i = 0; i < response.data.data.sh_items.length; i++ ) {
          response.data.data.sh_items[i].play_video_flag = true
        }
        // console.log(response.data.data.sh_items)
        if (response.data.code == 0 && response.data.data.sh_items.length > 0) {
          that.setData({
            video_listflag: true,
            video_has_more: true,
            video_list: that.data.video_list.concat(response.data.data.sh_items)
          })
          if (that.data.video_list.length == response.data.data.sh_total_rows) {
            that.setData({
              video_listflag: false,
              video_has_more: false
            })
          } 
          // console.log(that.data.video_list)
        } else {
          that.setData({
            video_listflag: false,
            video_has_more: false
          })
        }
        that.setData({
          showLoading: false
        })
        wx.stopPullDownRefresh()
      }, function (error) {
        console.log(error);
      });
  },
  videoPlay: function (e) {
    var curIdx = e.currentTarget.dataset.index;
    // 没有播放时播放视频
    if (!this.data.playIndex) {
      this.setData({
        playIndex: curIdx
      })
      var videoContext = wx.createVideoContext('video' + curIdx) //这里对应的视频id
      videoContext.play()
    } else { // 有播放时先将prev暂停，再播放当前点击的current
      var videoContextPrev = wx.createVideoContext('video' + this.data.playIndex)
      if (this.data.playIndex != curIdx) {
        videoContextPrev.pause()
      }
      this.setData({
        playIndex: curIdx
      })
      var videoContextCurrent = wx.createVideoContext('video' + curIdx)
      videoContextCurrent.play()
    }
  },
  previewImg: function (e) {
    var that = this, commentidx = e.currentTarget.dataset.commentidx,
      imgidx = e.currentTarget.dataset.imgidx,
      imgs = this.data.material_list[commentidx].sh_images;
    
    wx.previewImage({
      current: imgs[imgidx],
      urls: imgs
    })
  },

  reproduce: function (e) {//一键复制
    var that = this, index = e.currentTarget.dataset.index,
      img_type = e.currentTarget.dataset.type,
      word = String(e.currentTarget.dataset.word),
      imgs = this.data.material_list[index].sh_images;
    wx.setClipboardData({
      data: word,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            if (res.data) {
              // shihan.showToast.call(that, '复制成功');
            }
          }
        })
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      video_page: 1,
      listflag: true,
      playIndex: null,
      video_listflag: true,
      showLoading: true,
      material_list: [],
      video_list: []
    })
    if (this.data.currentTab == 1) {
      this.getVideoList();
    } else {
      this.getMaterialList();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.currentTab == 1) {
      if (this.data.video_listflag) {
        this.setData({
          video_listflag: false,
          video_page: this.data.video_page + 1
        })
        this.getVideoList();
      }
    } else {
      if (this.data.listflag) {
        this.setData({
          listflag: false,
          page: this.data.page + 1
        })
        this.getMaterialList()
      }
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var customerId = wx.getStorageSync('customerId'), 
      index = options.target.dataset.index, 
      sharetit = this.data.video_list[index].sh_share_data.sh_program_title, 
      shareimg = this.data.video_list[index].sh_share_data.sh_program_image,
      url = this.data.video_list[index].sh_share_data.sh_program_url;
    return {
      title: sharetit,
      path: url,
      imageUrl: shareimg,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})